//
//  AddNewAddressViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AddNewAddressViewController.h"
#import "LocationNearbyViewController.h"
#import "LocationData.h"
#import "MyAddressViewController.h"
#import "LocationCityViewController.h"
@interface AddNewAddressViewController ()
{
    NSString *_sex;
    LocationData *_data;
    UIButton *_btn;
}

@end

@implementation AddNewAddressViewController

- (void)viewDidLoad {
    self.title=@"地址新增";
    [super viewDidLoad];
    _sex=@"1";
    [self.city setTitle:CURRENTCITY forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark--进来和出去的一些设置
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
    
}
#pragma mark--选择城市
- (IBAction)goToSelectCity:(id)sender
{
    LocationCityViewController *City=[[LocationCityViewController alloc]init];
    City.currentCity=CURRENTCITY;
    typeof(self) weakSelf=self;
    City.passCityBlock=^(NSString *cityName,NSString *cityCode)
    {
        [weakSelf.city setTitle:cityName forState:UIControlStateNormal];
        [weakSelf.areaAddress setTitle:@"点击选择" forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:City animated:YES];
}

#pragma mark--确认添加校检
- (IBAction)sureToAddAddress:(id)sender {

    if ([[self.city titleForState:UIControlStateNormal]isEqualToString:@"定位失败"]) {
        [MBProgressHUD showError:@"请选择城市！"];
    }else if ([[self.city titleForState:UIControlStateNormal]isEqualToString:@"点击选择"])
    {
        [MBProgressHUD showError:@"请选择小区/大厦/学校"];

    }else if (self.doorNumber.text.length==0)
    {
        [MBProgressHUD showError:@"楼号－门牌号不能为空哦～"];
         [self.doorNumber becomeFirstResponder];

    }else if (self.name.text.length==0)
    {
        [MBProgressHUD showError:@"姓名不能为空哦～"];
        [self.name becomeFirstResponder];
        
    }else if (self.phoneNumber.text.length==0)
    {
        [MBProgressHUD showError:@"手机号不能为空哦～"];

    }else if ([GeneralTool valiMobile:self.phoneNumber.text]!=nil)
    {
        [MBProgressHUD showError:[GeneralTool valiMobile:self.phoneNumber.text]];
        //不符合格式的手机号先清空
        self.phoneNumber.text=@"";
        //将光标移到输入手机号
        [self.phoneNumber becomeFirstResponder];
    }else
    {
        //添加地址请求
        [self addAddressRequest];
    }
}

- (IBAction)clickOnMan:(id)sender {
    UIButton *btn=sender;
    _sex=@"1";
    [btn setBackgroundImage:[UIImage imageNamed:@"pay_true"] forState:UIControlStateNormal];
    [self.selectedWoman setBackgroundImage:[UIImage imageNamed:@"pay_false"] forState:UIControlStateNormal];
}
#pragma mark--添加地址请求
-(void)addAddressRequest
{
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSString *areaAddress=[self.areaAddress titleForState:UIControlStateNormal];
    NSString *cityAddress=[self.city titleForState:UIControlStateNormal];
    NSString *Longitude=[NSString stringWithFormat:@"%f",_data.Longitude];
    NSString *Latitude=[NSString stringWithFormat:@"%f",_data.Latitude];
    NSDictionary *dic=@{@"AddressId":@"0",
                        @"UserCode":GETUSERCODE,
                        @"ReceiveName":self.name.text,
                        @"Mobile":self.phoneNumber.text,
                        @"AreaCode":_data.Areacode,
                        @"LocateAddress":areaAddress,
                        @"Address":cityAddress,
                        @"Longitude":Longitude,
                        @"Latitude":Latitude,
                        @"IsDefault":@"0",
                        @"Sex":_sex,
                        @"DetailAddress":self.doorNumber.text,
                        @"Timestamp":timeStamp};

    NSString *sign=[GeneralTool GetSin:dic];
    NSDictionary *dataDic=@{@"AddressId":@"0",
                            @"UserCode":GETUSERCODE,
                            @"ReceiveName":self.name.text,
                            @"Mobile":self.phoneNumber.text,
                            @"AreaCode":_data.Areacode,
                            @"LocateAddress":areaAddress,
                            @"Address":cityAddress,
                            @"Longitude":Longitude,
                            @"Latitude":Latitude,
                            @"IsDefault":@"0",
                            @"Sex":_sex,
                            @"DetailAddress":self.doorNumber.text,
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
    NSDictionary *requestDic=@{@"m_UserAddr":dataDic};
    [RequestTool requestDataWithType:@"post" andUrlString:@"User/AddAddr" andParameters:requestDic andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showSuccess:@"请求失败，请检查网络"];
        }else
        {
            if ([responses[@"IsSuccess"]intValue]==1) {
                [MBProgressHUD showSuccess:[responses[@"ReturnString"]stringByRemovingPercentEncoding]];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else
            {
              [MBProgressHUD showSuccess:[responses[@"ReturnString"]stringByRemovingPercentEncoding]];
            }
            
        }
    }];
   
}
#pragma mark--点击选择小区／大厦
- (IBAction)goToSelectAddress:(id)sender {
    if ([[GeneralTool isConnectNet]isEqualToString:@"无网络"]) {
        [MBProgressHUD showError:@"网络连接失败，请检查网络"];
        return;
    }
    LocationNearbyViewController *location=[[LocationNearbyViewController alloc]init];
    location.hidesBottomBarWhenPushed=YES;
    location.categoryID=_categoryID;
    typeof(self) weakSelf=self;
    location.passLocationBlock=^(LocationData *data)
    {
        [weakSelf.areaAddress setTitle:data.name forState:UIControlStateNormal];
        _data=data;
    };
    [self.navigationController pushViewController:location animated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (IBAction)clickOnWoman:(id)sender {
    UIButton *btn=sender;
    _sex=@"2";
    [btn setBackgroundImage:[UIImage imageNamed:@"pay_true"] forState:UIControlStateNormal];
    [self.selectedMan setBackgroundImage:[UIImage imageNamed:@"pay_false"] forState:UIControlStateNormal];
}
@end
